﻿Public Class xtf_ArbolComposicion
    Dim obj_articulo As cls_articulo_

    Public Sub New(ByRef prm_articulo As cls_articulo_)
        InitializeComponent()
        obj_articulo = prm_articulo
        Me.Text = "Arbol de composicion - " & obj_articulo.ToString
        Me.btn_Guardar.Visible = False
        CrearArbol()
    End Sub

    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub



    Private Sub CrearArbol()
        If IsNothing(gdc_arbol.DataSource) Then
            Dim dtt_arbol As New DataTable
            AbrirColumnas2(obj_articulo, dtt_arbol, 0)
            dtt_arbol.Columns.Add("Material", GetType(Decimal))
            dtt_arbol.Columns.Add("Personal", GetType(Decimal))
            dtt_arbol.Columns.Add("Maquinaria", GetType(Decimal))
            dtt_arbol.Columns.Add("Mantenimiento", GetType(Decimal))
            dtt_arbol.Columns.Add("Total", GetType(Decimal))
            gdc_arbol.DataSource = dtt_arbol
            gdc_arbol.ForceInitialize()
            gdv_arbol.PopulateColumns()
            PerzonalizarControles(lac_general.Items)
            CrearRows2(obj_articulo, Nothing, dtt_arbol, 0)
            gdv_arbol.BestFitColumns()
            gdv_arbol.Columns("Material").OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False
            gdv_arbol.Columns("Material").SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum
            gdv_arbol.Columns("Material").SummaryItem.DisplayFormat = "{0:n" & int_cantdecimales & "}"
            gdv_arbol.Columns("Personal").OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False
            gdv_arbol.Columns("Personal").SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum
            gdv_arbol.Columns("Personal").SummaryItem.DisplayFormat = "{0:n" & int_cantdecimales & "}"
            gdv_arbol.Columns("Maquinaria").OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False
            gdv_arbol.Columns("Maquinaria").SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum
            gdv_arbol.Columns("Maquinaria").SummaryItem.DisplayFormat = "{0:n" & int_cantdecimales & "}"
            gdv_arbol.Columns("Mantenimiento").OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False
            gdv_arbol.Columns("Mantenimiento").SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum
            gdv_arbol.Columns("Mantenimiento").SummaryItem.DisplayFormat = "{0:n" & int_cantdecimales & "}"
            gdv_arbol.Columns("Total").OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False
            gdv_arbol.Columns("Total").SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum
            gdv_arbol.Columns("Total").SummaryItem.DisplayFormat = "{0:n" & int_cantdecimales & "}"
            gdv_arbol.UpdateSummary()
        End If
    End Sub

    Private Sub CrearRows2(ByRef prm_articulo As cls_articulo_, ByRef prm_dtr As DataRow, ByRef prm_dtt As DataTable, ByRef prm_nivel As Integer, Optional prm_articulocomposicionarticulo As cls_articulocomposicionarticulo_ = Nothing)
        Dim MiROW As DataRow = prm_dtt.NewRow
        If Not IsNothing(prm_dtr) Then
            For Each col As DataColumn In prm_dtt.Columns
                If col.ColumnName.Contains("A") Then
                    MiROW(col) = prm_dtr(col)
                End If
            Next
        End If
        MiROW("A" & prm_nivel) = prm_articulo.nombre
        If Not IsNothing(prm_articulocomposicionarticulo) Then
            MiROW("Material") = Math.Round(prm_articulocomposicionarticulo.cantidad * prm_articulocomposicionarticulo.valor, int_cantdecimales)
            MiROW("Total") = MiROW("Material")
        Else
            MiROW("Material") = prm_articulo.costo
            MiROW("Total") = prm_articulo.costo
        End If

        If IsNothing(prm_articulo.articulocomposicionarticulo_) Then
            If IsNothing(prm_articulo.articulocomposicionmaquinariatarea_) Then
                If IsNothing(prm_articulo.articulocomposicionproceso_) Then
                    prm_dtt.Rows.Add(MiROW)
                End If
            End If
        End If

        If Not IsNothing(prm_articulo.articulocomposicionarticulo_) Then
            If Not IsNothing(prm_articulo.articulocomposicionarticulo_.Items) Then
                For Each comp As cls_articulocomposicionarticulo_ In prm_articulo.articulocomposicionarticulo_.Items
                    CrearRows2(comp.articulo, MiROW, prm_dtt, prm_nivel + 1, comp)
                Next
            End If
        End If
        If Not IsNothing(prm_articulo.articulocomposicionmaquinariatarea_) Then
            If Not IsNothing(prm_articulo.articulocomposicionmaquinariatarea_.Items) Then
                For Each comp As cls_articulocomposicionmaquinariatarea_ In prm_articulo.articulocomposicionmaquinariatarea_.Items
                    CrearRowsMaquinariaTarea(comp, MiROW, prm_dtt, prm_nivel + 1)
                Next
            End If
        End If
        If Not IsNothing(prm_articulo.articulocomposicionproceso_) Then
            If Not IsNothing(prm_articulo.articulocomposicionproceso_.Items) Then
                For Each comp As cls_articulocomposicionproceso_ In prm_articulo.articulocomposicionproceso_.Items
                    CrearRowsProcesos(comp, MiROW, prm_dtt, prm_nivel + 1)
                Next
            End If
        End If
    End Sub

    Private Sub CrearRowsProcesos(ByRef prm_proceso As cls_articulocomposicionproceso_, ByRef prm_dtr As DataRow, ByRef prm_dtt As DataTable, ByRef prm_nivel As Integer)
        Dim MiROW As DataRow = prm_dtt.NewRow
        If Not IsNothing(prm_dtr) Then
            For Each col As DataColumn In prm_dtt.Columns
                If col.ColumnName.Contains("A") Then
                    MiROW(col) = prm_dtr(col)
                End If
            Next
        End If
        MiROW("A" & prm_nivel) = prm_proceso.proceso.ToString
        MiROW("Personal") = Math.Round(prm_proceso.cantidad * prm_proceso.valor, int_cantdecimales)
        MiROW("Total") = MiROW("Personal")
        prm_dtt.Rows.Add(MiROW)
    End Sub

    Private Sub CrearRowsMaquinariaTarea(ByRef prm_maquinariatarea As cls_articulocomposicionmaquinariatarea_, ByRef prm_dtr As DataRow, ByRef prm_dtt As DataTable, ByRef prm_nivel As Integer)
        Dim MiROW As DataRow = prm_dtt.NewRow
        If Not IsNothing(prm_dtr) Then
            For Each col As DataColumn In prm_dtt.Columns
                If col.ColumnName.Contains("A") Then
                    MiROW(col) = prm_dtr(col)
                End If
            Next
        End If
        MiROW("A" & prm_nivel) = prm_maquinariatarea.maquinariatarea.ToString
        MiROW("Maquinaria") = Math.Round(prm_maquinariatarea.cantidad * prm_maquinariatarea.valor, int_cantdecimales)
        MiROW("Mantenimiento") = Math.Round(prm_maquinariatarea.cantidad * prm_maquinariatarea.valorMantenimiento, int_cantdecimales)
        MiROW("Total") = Math.Round(MiROW("Maquinaria") + MiROW("Mantenimiento"), int_cantdecimales)
        prm_dtt.Rows.Add(MiROW)
    End Sub

    Private Sub AbrirColumnas2(ByRef prm_articulo As cls_articulo_, ByRef prm_dtt As DataTable, ByRef prm_nivel As Integer)
        If IsNothing(prm_dtt.Columns("A" & prm_nivel)) Then
            prm_dtt.Columns.Add("A" & prm_nivel)
            prm_dtt.Columns("A" & prm_nivel).Caption = "Nivel " & prm_nivel
        End If

        If Not IsNothing(prm_articulo.articulocomposicionarticulo_) Then
            If Not IsNothing(prm_articulo.articulocomposicionarticulo_.Items) Then
                For Each comp As cls_articulocomposicionarticulo_ In prm_articulo.articulocomposicionarticulo_.Items
                    AbrirColumnas2(comp.articulo, prm_dtt, prm_nivel + 1)
                Next
            End If
        End If
    End Sub

    Private Sub gdv_arbol_CellMerge(sender As Object, e As DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs) Handles gdv_arbol.CellMerge
        If e.Column.ColumnType.ToString <> GetType(Decimal).ToString Then
            '    If (e.Column.Name.Contains("A")) Then

            '        Dim view As DevExpress.XtraGrid.Views.Grid.GridView = CType(sender, DevExpress.XtraGrid.Views.Grid.GridView)
            '        Dim val1 As String
            '        If IsDBNull(view.GetRowCellValue(e.RowHandle1, e.Column)) Then
            '            val1 = ""
            '        Else
            '            val1 = view.GetRowCellValue(e.RowHandle1, e.Column)
            '        End If
            '        Dim val2 As String
            '        If IsDBNull(view.GetRowCellValue(e.RowHandle2, e.Column)) Then
            '            val2 = ""
            '        Else
            '            val2 = view.GetRowCellValue(e.RowHandle2, e.Column)
            '        End If
            '        e.Merge = (val1 = val2)
            '        e.Handled = True
            '    Else
            '        e.Merge = False
            '    End If
            'Else
            e.Merge = False
        End If
    End Sub

    Private Sub gdv_arbol_CustomDrawCell(sender As Object, e As DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs) Handles gdv_arbol.CustomDrawCell
        'If e.Column.Name = "colMaterial" Then
        '    e.Appearance.BackColor = Color.Silver
        'End If
        'If e.Column.Name = "colPersonal" Then
        '    e.Appearance.BackColor = Color.WhiteSmoke
        'End If
        'If e.Column.Name = "colMaquinaria" Then
        '    e.Appearance.BackColor = Color.Silver
        'End If
        'If e.Column.AppearanceCell.BackColor  "colTotal" Then

        'End If
        'e.Appearance.BackColor = Color.Red 'e.Column.AppearanceCell.BackColor
    End Sub

    Private Sub gdv_arbol_CustomDrawFooterCell(sender As Object, e As DevExpress.XtraGrid.Views.Grid.FooterCellCustomDrawEventArgs) Handles gdv_arbol.CustomDrawFooterCell
        'If e.Column.Name = "colMaterial" Then
        '    e.Appearance.BackColor = Color.Silver
        'End If
        'If e.Column.Name = "colPersonal" Then
        '    e.Appearance.BackColor = Color.WhiteSmoke
        'End If
        'If e.Column.Name = "colMaquinaria" Then
        '    e.Appearance.BackColor = Color.Silver
        'End If
        'If e.Column.Name = "colTotal" Then
        '    e.Appearance.BackColor = Color.WhiteSmoke
        'End If
    End Sub
End Class